package com.kuang.demo.service.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuang.demo.bo.wx.WxclearHistoryBO;
import com.kuang.demo.mapper.wx.WxKeyWordMapper;
import com.kuang.demo.pojo.wx.Cskaoyanmall_keyword;
import com.kuang.demo.vo.wx.WxQuerySearchIndex;
import com.kuang.demo.vo.wx.WxSearchHelperVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 郭浩 on 2020/11/5 20:13
 */
@Service
public class WxkeyWordServiceImpl implements WxKeyWordService{
    @Autowired
    WxKeyWordMapper wxKeyWordMapper;
    @Override
    public WxQuerySearchIndex getSearchIndex() {
        WxQuerySearchIndex wxQuerySearchIndex = new WxQuerySearchIndex();
        //获得历史记录得前五条
        QueryWrapper<Cskaoyanmall_keyword> historyWrapper = new QueryWrapper<>();
        historyWrapper.orderByDesc("update_time").ne("deleted",1).eq("is_hot",0);
        Page<Cskaoyanmall_keyword> historyPage = wxKeyWordMapper.selectPage(new Page<>(1, 7), historyWrapper);
        List<Cskaoyanmall_keyword> historyKeywordList = historyPage.getRecords();
        //通过历史记录设置历史搜索和默认搜索
        wxQuerySearchIndex.setHistoryKeywordList(historyKeywordList);
        wxQuerySearchIndex.setDefaultKeyword(historyKeywordList.get(0));
        //获得热搜
        QueryWrapper<Cskaoyanmall_keyword> hotWrapper = new QueryWrapper<>();
        hotWrapper.eq("is_hot",1).ne("deleted",1);
        Page<Cskaoyanmall_keyword> hotPage = wxKeyWordMapper.selectPage(new Page<>(1, 7), hotWrapper);
        List<Cskaoyanmall_keyword> hotKeywordList = hotPage.getRecords();
        wxQuerySearchIndex.setHotKeywordList(hotKeywordList);


        return wxQuerySearchIndex;
    }

    @Override
    public WxSearchHelperVO searchHelp(String keyword) {
        QueryWrapper<Cskaoyanmall_keyword> wrapper = new QueryWrapper<>();
        wrapper.like("keyword",keyword);
        List<Cskaoyanmall_keyword> cskaoyanmall_keywords = wxKeyWordMapper.selectList(wrapper);
        WxSearchHelperVO wxSearchHelperVO = new WxSearchHelperVO();
        List<String> keywords = new ArrayList<>();
        for (int i = 0; i < cskaoyanmall_keywords.size(); i++) {
            String keyword1 = cskaoyanmall_keywords.get(i).getKeyword();
            keywords.add(keyword1);
        }
        wxSearchHelperVO.setData(keywords);
        return wxSearchHelperVO;

    }

    @Override
    public void clearHisttory(WxclearHistoryBO wxclearHistoryBO) {
        QueryWrapper<Cskaoyanmall_keyword> wrapper = new QueryWrapper<>();
        wrapper.ne("is_hot",1);
        Cskaoyanmall_keyword cskaoyanmall_keyword = new Cskaoyanmall_keyword();
        cskaoyanmall_keyword.setDeleted(1);
        wxKeyWordMapper.update(cskaoyanmall_keyword,wrapper);
    }
}
